package com.cn.longdistancebusstation.contacter;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.util.RegularCheckUtil;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/18.
 */

public class AddNewPassengersActivity extends BaseActivity implements View.OnClickListener {
    private EditText mNameEditText, mCardNoEditText,mTelephoneNumberEditText;
    private TextView mTypeTextView;
    private Button btn_conservation;
    private String type="0";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_passengers);
        getTitleTextView().setText("新增联系人");
        initView();

    }

    private void initView() {
        mNameEditText = (EditText) findViewById(R.id.name_of_passengers);
        mNameEditText.setOnClickListener(this);
        mCardNoEditText = (EditText) findViewById(R.id.idcard_numbers);
        mCardNoEditText.setOnClickListener(this);
        mTypeTextView = (TextView) findViewById(R.id.select_linkman_type);
        mTypeTextView.setText("成人");
        mTelephoneNumberEditText=(EditText)findViewById(R.id.telephone_numbers);
        mTelephoneNumberEditText.setOnClickListener(this);
        btn_conservation = (Button) findViewById(R.id.conservation);
        btn_conservation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.conservation:
                toSave();
                break;
        }

    }

    private void showPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_linkman_type, null);
        final TextView tv_adult = (TextView) view.findViewById(R.id.tv_adult);
        final TextView tv_child = (TextView) view.findViewById(R.id.tv_child);
        final TextView tv_student = (TextView) view.findViewById(R.id.tv_student);
        final TextView tv_soldier = (TextView) view.findViewById(R.id.tv_soldier);
        TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        popupWindow.setOutsideTouchable(true);
        View parent = LayoutInflater.from(this).inflate(R.layout.activity_add_new_passengers, null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        //popupWindow在弹窗的时候背景半透明
        final WindowManager.LayoutParams params = getWindow().getAttributes();
        params.alpha = 0.5f;
        getWindow().setAttributes(params);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                getWindow().setAttributes(params);
            }
        });
        tv_adult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeTextView.setText(tv_adult.getText().toString());
                type = "0";
                popupWindow.dismiss();
            }
        });
        tv_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeTextView.setText(tv_child.getText().toString());
                type = "1";
                popupWindow.dismiss();

            }
        });
        tv_student.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeTextView.setText(tv_student.getText().toString());
                type = "2";
                popupWindow.dismiss();

            }
        });
        tv_soldier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTypeTextView.setText(tv_soldier.getText().toString());
                type = "3";
                popupWindow.dismiss();

            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    /*
    * 确认保存联系人信息
    * */
    private void toSave() {
        String strName = mNameEditText.getText().toString().trim();
        String strCardNo = mCardNoEditText.getText().toString().trim();
        String strType = mTypeTextView.getText().toString();
        String telephone=mTelephoneNumberEditText.getText().toString();
        if ("".equals(strName) || "".equals(strCardNo) || "".equals(strType)||"".equals(telephone)) {
            Toast.makeText(AddNewPassengersActivity.this, "姓名、身份证号和人员类型不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!RegularCheckUtil.checkCardNo(strCardNo)) {
            Toast.makeText(AddNewPassengersActivity.this, "请输入有效的证件号码", Toast.LENGTH_SHORT).show();
            return;
        }
        showHud();
        Retrofit retrofit = new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService service = retrofit.create(HttpService.class);
        Call<Result> call = service.addContacter(strName,strCardNo,telephone,type,GlobalVariable.getUserID());
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                hideHud();
                Gson gson = new Gson();
                Result result = response.body();
                Boolean isSuccess = result.getHead().getSuccess();
                if (isSuccess) {
                    Toast.makeText(AddNewPassengersActivity.this, "新增联系人成功", Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putString("select_passenger_type", type);
                    finish();
                } else {
                    Toast.makeText(AddNewPassengersActivity.this, result.getHead().getMsg(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                hideHud();
                Toast.makeText(AddNewPassengersActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
